﻿using MESPro.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MESPro.DBManager
{
    /// <summary>
    /// 数据库上下文
    /// </summary>
    public class DbContext
    {

        #region 字段

        /// <summary>
        /// todo 连接字符串,应该来自于配置文件
        /// 重庆:222.180.242.134,77
        /// </summary>
        public static string conString = "Server = .;Database = ManagementServer;User ID = sa;Password = 1;";
        //public static string conString = "Server = 222.180.242.134,77;Database = ManagementServer;User ID = shproject;Password = shouhang;";
        /// <summary>
        /// 数据库操作
        /// </summary>
        public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
        {
            ConnectionString = conString,//Trusted_Connection = False; 连接符字串
            DbType = DbType.SqlServer,//数据库类型
            IsAutoCloseConnection = true, //不设成true要手动close
            MoreSettings = new ConnMoreSettings()
            {
                SqlServerCodeFirstNvarchar = true,
            },
            ConfigureExternalServices = new ConfigureExternalServices()
            {
                //EntityService = (x, p) => //处理列名
                //{
                //    
                //p.DbColumnName = UtilMethods.ToUnderLine(p.DbColumnName);//ToUnderLine驼峰转下划线方法
                //},
                EntityNameService = (x, p) => //处理表名,系统基础表都以Sys开头
                {
                    p.DbTableName = "Sys_" + p.DbTableName;
                },

                EntityService = (c, p) =>
                {
                    // 值类型可空支持
                    if (p.IsPrimarykey == false && c.PropertyType.IsGenericType &&
                    c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                    {
                        p.IsNullable = true;
                    }
                }
            }
        },
db =>
{
    //(A)全局生效配置点
    //调试SQL事件，可以删掉
    db.Aop.OnLogExecuting = (sql, pars) =>
    {
        Debug.WriteLine(sql);//输出sql,查看执行sql
                             //5.0.8.2 获取无参数化 SQL
                             //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
    };
});

        #endregion 字段

        #region Public 方法

        /// <summary>
        /// 初始化表
        /// </summary>
        public static void InitTable()
        {
            try
            {
                Db.CodeFirst.InitTables(
            typeof(Model),//o to n
            typeof(FunctionGroup),
            typeof(Function),
            typeof(DbControl),
            typeof(DisLanguage),
            typeof(User),//Program
            typeof(Role),
            typeof(ProgramInfo),
            typeof(UserRole),
            typeof(RelationFunctionAndFunctionGroup),
            typeof(RoleFunction)
           );
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            MessageBox.Show("创建成功");
        }

        #endregion Public 方法

    }
}
